import { ethers } from "ethers";
import { providers } from "../rpcs";

const FEE_ADDRESS = "0x420000000000000000000000000000000000000F";
const FEE_ABI = [
  {
    inputs: [{ internalType: "bytes", name: "_data", type: "bytes" }],
    name: "getL1Fee",
    outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
    stateMutability: "view",
    type: "function",
  },
];

export const getOptimismFee = async (txData) => {
  const provider = providers.optimism;
  const gasContract = new ethers.Contract(FEE_ADDRESS, FEE_ABI, provider);

  try {
    const gas = await gasContract.getL1Fee(txData);
    return gas / 1e18;
  } catch (e) {
    console.log(e);
    return "Unknown";
  }
};
